package com.example.service.reporeReflection.impl;


import com.example.common.util.SpringContextUtil;
import com.example.entity.hz.Scraporder;
import com.example.entity.report.reportReflection.Audit;
import com.example.entity.report.reportReflection.Report;
import com.example.exception.CustomException;
import com.example.service.ScraporderService;
import com.example.service.ZnxService;
import com.example.service.reporeReflection.RootService;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

public class ScrapOrderImpl extends RootService {

    private ScraporderService service;

    public ScrapOrderImpl() {
        service = SpringContextUtil.getBean(ScraporderService.class);
        super.znxService = SpringContextUtil.getBean(ZnxService.class);
    }

    @Override
    public void report(Report report) {
        update(report.getDocumentNo(),0);
        //发送消息通知
        String message = String.format(REPORT_MESSAGE_MOLD,
                report.getWorkflowConfigName(),report.getDocumentNo(),
                report.getRemark(),report.getTime());
        sendMes(report.getMembers(),message,report.getDocumentNo());
    }

    @Override
    public void unReport(Report report) {
        update(report.getDocumentNo(),1);
        //发送消息通知
        String message=String.format(UN_REPORT_MESSAGE_MOLD,
                report.getWorkflowConfigName(),report.getDocumentNo(),
                report.getTime());
        sendMes(report.getMembers(),message,report.getDocumentNo());
    }

    @Override
    public void audit(Audit audit) {
        //判断是否通过
        String message;
        if (audit.getApprovalResult().equals("N")){
            //拒绝，给上报人返回审核失败通知，并返回失败原因，修改单据状态
            update(audit.getDocumentNo(),2);
            //发送拒绝通知
            message=String.format(AUDIT_FAIL_MESSAGE_MOLD,
                    audit.getWorkflowConfigName(),audit.getDocumentNo(),
                    audit.getApproverName(),audit.getApprovalRemark(),
                    audit.getApprovalTime());
        }else if (audit.getIsLast().equals("Y")){
            //通过，判断是否是最后一级  是：通知上报人审核通过,修改单据状态,设置报废时间
            update(audit.getDocumentNo(),3);
            //发送审核成功通知通知
            message=String.format(AUDIT_SUCCESS_MESSAGE_MOLD,
                    audit.getWorkflowConfigName(),audit.getDocumentNo(),
                    audit.getApprovalTime());
        }else {
            //通过，判断是否是最后一级  否：通知下一级的人,不触发修改状态方法
            //向下一级发送通知
            message = String.format(REPORT_MESSAGE_MOLD,
                    audit.getWorkflowConfigName(),audit.getDocumentNo(),
                    audit.getApprovalRemark(),audit.getApprovalTime());
        }
        sendMes(audit.getNextMembers(),message,audit.getDocumentNo());
    }

    /**
     * 公共状态更新方法
     * @param type 0-上报  1-取消上报  2-审核失败  3-审核成功通过
     */
    private void update(String documentNo,Integer type){
        String[] split = documentNo.split(super.SEPARATOR);
        String id = split[0];
        String number = split[1];
        Scraporder one = service.lambdaQuery().eq(Scraporder::getOrderType, id)
                .eq(Scraporder::getOrderNumber, number).one();
        if (one==null) throw new CustomException("500","不存在的报废单!");
        //修改状态
        switch (type){
            case 0:
                one.setAuditStatus("R");
                break;
            case 1:
                one.setAuditStatus("N");
                break;
            case 2:
                one.setAuditStatus("F");
                break;
            case 3:
                one.setAuditStatus("Y");
                break;
        }
        service.lambdaUpdate().eq(Scraporder::getOrderType, id)
                .eq(Scraporder::getOrderNumber, number).update(one);
        //取消上报和审核通过需要特判,修改报废时间
        if (type==1 || type==3){
            String bfTime=type==1 ? null:LocalDate.now().toString();
            service.lambdaUpdate().eq(Scraporder::getOrderType, id)
                    .eq(Scraporder::getOrderNumber, number)
                    .set(Scraporder::getEndTime,bfTime).update();
        }
    }

    /**
     * 发送消息方法
     * @param members  所有要接收消息的人
     * @param message 消息内容
     * @param document 单别+单号
     */
    private void sendMes(List<Map<String,String>> members,String message,String document){
        for (Map<String, String> member : members) {
            String uId = member.get("approverNo");
            String uName = member.get("approverName");
            super.znxService.saveZnxAndSendMessageBuZouPeiZhi(uId,message,TITLE,document);
        }
    }

    /**
     * 自定义审批处理方法
     * @param audit
     */
    public void myAudit1(Audit audit){

    }
}
